# coding: utf-8
""" ndarray数组的布尔索引和花式索引
"""
import numpy as np
from helpers import print_20_, print2

x = np.array([3,2,3,1,3,0])
y = np.array([True,False,True,False,True,False])  # 布尔型数组的长度必须跟被索引的轴长度一致
print_20_()
print2("01.布尔值索引")
print2("print(x) ->", x)
print2("print(y) ->", y)
print2("print(x[y]) ->", x[y])
print2("print(x[y==False]) ->", x[y==False])
print2("print(x[y==True]) ->", x[y==True])
print2("print(x>=3) ->", x>=3)
print2("print(x[x>=3]]) ->", x[x>=3])
print2("print(x[~(x>=3)]) ->", x[~(x>=3)])
print2("print((x==2)|(x==1)) ->", (x==2)|(x==1))
print2("print(x[(x==2)|(x==1)]) ->", x[(x==2)|(x==1)])
print_20_()
x[(x==2)|(x==1)] = 0
print2("print(x) ->", x)
print_20_()

print2("02.ndarray的花式索引:使用整型数组作为索引")
x = np.array([1,2,3,4,5,6])
print2("print(x) ->", x)
print2("print(x[[0,1,2]]) ->", x[[0,1,2]])
print2("print(x[[-1,-2,-3]]) ->", x[[-1,-2,-3]])

print_20_()
x = np.array([[1,2],[3,4],[5,6]])
print2("print(x) ->", x)
print2("print(x[[0,1]]) ->", x[[0,1]])
print2("print(x[[0,1],[0,1]]) ->", x[[0,1], [0,1]], "# 打印x[0][0]和x[1][1]")
print2("print(x[[0,1],[0,0]]) ->", x[[0,1], [0,0]], "# 打印x[0][0]和x[1][0]")
print2("print(x[[0,1]][:,[0,1]]) ->", x[[0,1]][:,[0,1]], "# ")
print2("print(x[[0,1]][1]) ->", x[[0,1]][0, 1], "# ")
print2("print(x[np.ix_([0,1,2],[0,1])]) ->", x[np.ix_([0,1,2],[0,1])], "# 使用numpy.ix_()函数增强可读性")
x[[0,1,2],[0,1,0]] = [0,0,0]
print2("print(x) ->", x)
print_20_()


